package com.topscomm.pm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.impl.PmCalendarService;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PmProjectCalendarScript extends PmScript {
    /**
     * 立项审批结束后生成项目日历（单独配置在结束节点预防重复审批问题）
     *
     * @param service
     * @param entity
     * @throws Exception
     */
    @Transactional
    public void creatProjectCalendar(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        PmProjectService pmProjectService = (PmProjectService) SpringUtil.getBean("pmProjectService");
        //更新项目状态
        Map<String, Object> updateParam = new HashedMap<>();
        updateParam.put("id", sourceid);
        updateParam.put("projectstate", 1);
        pmProjectService.update(updateParam);
        // 立项通过后生成项目日历
        PmCalendarService pmCalendarService = (PmCalendarService) SpringUtil.getBean("pmCalendarService");
        PmProjectEntity pmProjectEntity = pmProjectService.queryById(sourceid);
        long projectId = 0L;
        String projectCode = "";
        Date planStartTime = null;
        Date planEndTime = null;
        if (pmProjectEntity != null) {
            projectId = pmProjectEntity.getId();//项目id
            projectCode = pmProjectEntity.getCode();//项目号
            planStartTime = pmProjectEntity.getPlanstarttime();//计划完工时间
            planEndTime = pmProjectEntity.getPlanendtime();//计划完工时间
        }
        Map<String, Object> map = new HashMap<>();
        map.put("projectid", projectId);
        map.put("projectcode", projectCode);
        map.put("starttime", planStartTime);
        map.put("endtime", planEndTime);
        pmCalendarService.insertBatchCalendar(map);
    }
}
